﻿namespace TemplateMethodMode;
/// <summary>
/// 模板方法设计模式
/// </summary>
internal class TemplateMethodModeClass
{
    static async Task testMain()
    {
        var context = new HttpContext()
        {
            Method = "Get"
        };

        IServlet servlet = new HelloServlet();
        await  servlet.InvokeAsync(context);
        
    }

}

public class HttpContext
{
    public string Method { get; set; }
    public string Request { get; set; }
    public string response { get; set; }
}

public interface IServlet
{
    Task InvokeAsync(HttpContext context);
}


/// <summary>
/// 模板方法设计模式   模板方法
/// </summary>
public abstract class httpServlet : IServlet
{
    public async Task InvokeAsync(HttpContext context)
    {
        if (context.Method == "Get")
        {
            await GetAsync(context);
        }
        else if (context.Method == "Post")
        { 
            await PostAsync(context);
        }
    }
    //public abstract Task GetAsync(HttpContext context);
    //public abstract Task PostAsync(HttpContext context);

    public virtual Task GetAsync(HttpContext context) { 
        return Task.CompletedTask;
    }
    public virtual Task PostAsync(HttpContext context) {
        return Task.CompletedTask;
    }
}


public  class HelloServlet : httpServlet
{
    public override Task GetAsync(HttpContext context)
    {
        Console.WriteLine("hello world");
        return Task.CompletedTask;
    }
}


